package cn.zmwh.manage.admin.modules.upms.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.constants.Constants;
import cn.zmwh.common.mybatis.page.PageRequest;
import cn.zmwh.common.security.annotation.PreRole;
import cn.zmwh.manage.admin.modules.upms.pojo.entity.SysRole;
import cn.zmwh.manage.admin.modules.upms.pojo.entity.SysRoleMenu;
import cn.zmwh.manage.admin.modules.upms.pojo.request.RelevanceRequest;
import cn.zmwh.manage.admin.modules.upms.pojo.request.SysRoleRequest;
import cn.zmwh.manage.admin.modules.upms.service.SysRoleMenuService;
import cn.zmwh.manage.admin.modules.upms.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色管理 前端控制器
 * </p>
 *
 * @author dmzmwh
 * @since 2025-03-11
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {


    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    public SysRoleMenuService sysRoleMenuService;

    @ApiOperation("获取用户角色,角色可以从token内解析")
    @GetMapping(value = "/user")
    public Result<Set<String>> user(@ApiIgnore @RequestHeader(Constants.UID) Long uid,
                                    @ApiIgnore @RequestHeader(Constants.USERNAME) String username) {
        Set<String> role = sysRoleService.listByUser(uid, username);

        return Result.ok(role);
    }


    /**
     * 添加角色
     *
     * @param sysRole 角色信息
     * @return success、false
     */
    @PostMapping
    @PreRole(role = "ADMIN", name = "维护角色")
    public Result save(@RequestBody SysRoleRequest req) {
        if (req.getId() == null) {
            req.setCreateTime(new Date());
        }
        req.setUpdateTime(new Date());
        sysRoleService.saveOrUpdate(req);
        Set<Long> menuIds = req.getMenuIds();
        if (CollUtil.isNotEmpty(menuIds)) {
            RelevanceRequest request = new RelevanceRequest();
            request.setRoleId(req.getId());
            request.setMenuIds(menuIds);
            sysRoleMenuService.relevance(request);
        }
        return Result.success();
    }


    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    @PreRole(role = "ADMIN", name = "删除角色")
    public Result removeById(@PathVariable Integer id) {
        sysRoleService.removeRoleById(id);
        return Result.success();
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping("/list")
    @ApiOperation("角色列表分页")
    public Result list(PageRequest request) {
        IPage<SysRole> page = sysRoleService.page(request);
        return Result.ok(page);
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping("/list/all")
    @ApiOperation("角色列表不分页")
    public Result listAll() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping("/detail/{id}")
    @ApiOperation("角色详情")
    public Result<SysRoleRequest> detail(@PathVariable Long id) {
        SysRole byId = sysRoleService.getById(id);
        if (byId == null) {
            return Result.failed("角色不存在");
        }
        SysRoleRequest sysRoleRequest = new SysRoleRequest();
        BeanUtil.copyProperties(byId, sysRoleRequest);
        List<SysRoleMenu> list1 = sysRoleMenuService.list(Wrappers.<SysRoleMenu>lambdaQuery()
                .eq(SysRoleMenu::getRoleId, id));
        if (CollUtil.isNotEmpty(list1)) {
            Set<Long> set = list1.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
            sysRoleRequest.setMenuIds(set);
        }
        return Result.ok(sysRoleRequest);
    }
}

